package com.lmx.smsmonitor.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreUtil {

    private static SharedPreUtil util;
    private static SharedPreferences sp;

    private SharedPreUtil(Context context, String name) {
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    public static void getInstance(Context context, String name) {
        if (util == null) {
            sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        }
    }

    public static boolean putData(String key, Object value) {
        SharedPreferences.Editor edit = sp.edit();
        String type = value.getClass().getSimpleName();
        switch (type) {
            case "Boolean":
                edit.putBoolean(key, (Boolean) value);
                break;
            case "Long":
                edit.putLong(key, (Long) value);
                break;
            case "Float":
                edit.putFloat(key, (Float) value);
                break;
            case "String":
                edit.putString(key, (String) value);
                break;
            case "Integer":
                edit.putInt(key, (Integer) value);
                break;
            default:
                return false;
        }
        edit.apply();
        return true;
    }

    public static Object getData(String key, Object defValue) {
        String type = defValue.getClass().getSimpleName();
        switch (type) {
            case "Boolean":
                return sp.getBoolean(key, (Boolean) defValue);
            case "Long":
                return sp.getLong(key, (Long) defValue);
            case "Float":
                return sp.getFloat(key, (Float) defValue);
            case "String":
                return sp.getString(key, (String) defValue);
            case "Integer":
                return sp.getInt(key, (Integer) defValue);
            default:
                return defValue;
        }
    }

}
